// webpack 打包vue项目过程中，分理出一点页面
const ManifestPlugin = require('webpack-manifest-plugin')
const nodeExternals = require('webpack-node-externals')

exports.chainWebpack = webpackConfig => {
  if (!process.env.SSR) {
    // 当前非SSR环境
    webpackConfig.devServer.disableHostCheck(true)
    return
  }

  // 让webpack以node适用的方式处理动态导入
  webpackConfig.entry('app').clear().add('./src/main.server.js')
  webpackConfig.target('node')
  // 使用node风格导出模块

  webpackConfig.output.libraryTarget('commonjs2')
  // 将服务器的整个输出构建为单个json文件的插件
  webpackConfig.plugin('manifest').use(new ManifestPlugin({ fileName: 'ssr-manifest.json' }))


  webpackConfig.externals(nodeExternals({allowlist:/\.(css|vue)$/}))
  webpackConfig.optimization.splitChunks(false).minimize(false)

  webpackConfig.plugins.delete('hmr')
  webpackConfig.plugins.delete('preload')
  webpackConfig.plugins.delete('progress')
  webpackConfig.plugins.delete('friendly-errors')
}